<?php
require_once("../connection.php");

$user  = $_POST['user'];
$val1 = $_POST['val'];
$val  = mysql_real_escape_string($_POST['val']);
$val2 = $val;
$val = htmlentities($val);  // for removing all tags except break
$val3 = $val;
$media = mysql_real_escape_string($_POST['media']);

$postas = $_POST['postas'];
$type = $_POST['type'];

date_default_timezone_set('Asia/Calcutta');
$time = time();

$arr = explode("_",$type);
$type1 = $arr[0];
$type2 = $arr[1];

if($type1!="pms")
{
$res = mysql_query("INSERT INTO site_posts (`type`,`user`,`postas`,`text`,`media`,`timestamp`,`freshness`) VALUES('$type','$user','$postas','$val','$media','$time','$time')");
if(!$res) echo mysql_error();

// for thee subscription part only for group posts
$id = mysql_insert_id();
echo "id_".$id;

//writing into a file
fwrite(fopen('../../chat/'.$type.'.xml', 'w'), $time);

//updating newness
$result = mysql_query("UPDATE site_group_newness SET `newness`='$time' where `gpid`='$type2'");		 
if (mysql_affected_rows()==0) {
	$result = mysql_query("INSERT into site_group_newness (`gpid`, `newness`) values ('$type2','$time')");
}

}
else {
		
		if($arr[1] == $user)
			$pattern = "10";
		else $pattern = "01";
		
		//echo $arr[1].';'.$user;
		
	$res = mysql_query("INSERT INTO site_pms (`type`,`fbid`,`name`,`text`,`timestamp`,`read`) VALUES('$type','$user','$postas','$val','$time','$pattern')");
if(!$res) echo mysql_error();

$id = mysql_insert_id();

//writing into the file



if(fwrite(fopen('../../chat/'.$type.'.xml', 'w'), $time))
{
//echo "written";
}
else
echo "not written";

echo "id_pms_".$id;


}

//echo $val1." ".$val2." ".$val3;

?>